#include "MysqlConnPtr.h"
#include "MysqlConnPool.h"

LoggerPtr MysqlConnPtr::_logger(Logger::getLogger("MysqlConnPtr"));

MysqlConnPtr::MysqlConnPtr() {
	_mysql = NULL;
	_index = -1;
	_pool = NULL;
}

int MysqlConnPtr::assign(MYSQL * mysql, int index, MysqlConnPool * pool) {
	if (NULL == mysql || index < 0 || NULL == pool) {
		LOG4CXX_ERROR(_logger, "assign error, invalid args, index:" << index);
		return -1;
	}
	if (NULL != _mysql) {
		LOG4CXX_ERROR(_logger, "assign error, can not assign twice");
		return -1;
	}
	_mysql = mysql;
	_index = index;
	_pool = pool;
}

MysqlConnPtr::~MysqlConnPtr() {
	if (NULL != _mysql) _pool->release(_index);
}

int MysqlConnPtr::get_index() { return _index; }

MYSQL * MysqlConnPtr::get() { return _mysql; }
